import { Injectable } from '@angular/core';
import { ApiService } from '@core/api.service';
import { ControlApiService } from '@core/controlApi.service';

@Injectable({ providedIn: 'root' })

export class StationDevtypeService {
    constructor(
        private apiService: ApiService,
        private controlApiService: ControlApiService
    ) { }

    // 获取车站列表
    getStation(callback) {
        this.apiService.getorgstatree().subscribe((res: any) => {
            callback(res);
        });
    }

    // 获取设备类型列表
    getDevType(stationId, callback) {
        if (!stationId) {
            return;
        }
        this.apiService.getAllTypesByStaId(stationId).subscribe((res: any) => {
            callback(res);
        });
    }

    // 获取报警类型列表
    getAlarmType(devTypeId, callback) {
        if (!devTypeId) {
            return;
        }
        this.controlApiService.getjcbjtype(devTypeId).subscribe((res: any) => {
            callback(res);
        });
    }

    // 获取设备名称列表
    getDevName(stationId, typeId, callback) {
        if (!stationId) {
            return;
        }
        this.controlApiService.getDevByStationType(stationId, typeId).subscribe((res: any) => {
            callback(res);
        });
    }

    // ID或TEXT互转判断(应用场景,比对ID传'id'反之,接口参数,比对值)
    FindIdText(scene, type, params, compare, typeId, callback) {
        switch (scene) {
            case 'station':
                this.getStation((resdata) => {
                    if (resdata.length) {
                        this.getIdorText(resdata, type, compare, callback);
                    }
                });
                break;
            case 'devName':
                this.getDevName(params, typeId, (resdata) => {
                    if (resdata.length) {
                        this.getIdorText(resdata, type, compare, callback);
                    }
                });
                break;
        }
    }
    // ID或TEXT互转逻辑
    getIdorText(resdata, type, compare, callback) {
        const tempObject: any = {};
        for (const item of resdata) {
            if (item.children) {
                for (const list of item.children) {
                    switch (type) {
                        case 'id':
                            if (list.id == compare) {
                                tempObject.text = list.text;
                                tempObject.id = list.id;
                                break;
                            }
                            break;
                        case 'text':
                            if (list.text == compare) {
                                tempObject.text = list.text;
                                tempObject.id = list.id;
                                break;
                            }
                            break;
                    }
                }
            } else {
                switch (type) {
                    case 'id':
                        if (item.id == compare) {
                            tempObject.text = item.text;
                            tempObject.id = item.id;
                            break;
                        }
                        break;
                    case 'text':
                        if (item.text == compare) {
                            tempObject.text = item.text;
                            tempObject.id = item.id;
                            break;
                        }
                        break;
                }
            }
        }
        if (callback) {
            callback(tempObject);
        }
    }
}
